﻿//https://leetcode.cn/problems/friends-of-appropriate-ages/

class Solution {
public:
    int numFriendRequests(vector<int>& ages)
    {
        int ret = 0;
        int left = 0, right = 0;
        int n = ages.size();
        sort(ages.begin(), ages.end());

        for (auto e : ages)
        {
            if (e < 15)
            {
                continue;
            }

            //出窗口
            while (ages[left] <= 0.5 * e + 7)
            {
                left++;
            }

            while (right + 1 < n && ages[right + 1] <= e)
            {
                ++right;
            }
            ret += right - left;
        }
        return ret;

    }
};